﻿/*
 * OpenDoPE Word Add-In
 * Copyright (C) 2010-2011  Plutext Pty Ltd
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * For details, see http://www.gnu.org/licenses/gpl-2.0.html
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NLog;

using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;


namespace OpenDoPE_Wed
{
    public partial class UserControlComponentIRI : UserControl
    {

        static Logger log = LogManager.GetLogger("OpenDoPE_Wed");

        WedTaskPane taskPane;

        OpenDoPE_Wed.components odComponents;
        OpenDoPE_Wed.componentsComponent existingComponent; 

        public UserControlComponentIRI(WedTaskPane taskPane)
            //Word.ContentControl currentCC, XmlEditorControl xec)
        {
            this.taskPane = taskPane;
            InitializeComponent();
        }

        public void initFields()
        {
            odComponents = new components();
            OpenDoPE_Wed.components.Deserialize(taskPane.componentsPart.XML, out odComponents);

            Word.ContentControl currentCC = taskPane.currentCC;
            TagData td = new TagData(currentCC.Tag);
            if (td.get("od:component") != null)
            {
                string id = td.get("od:component");

                existingComponent = getComponentById(id);
                if (existingComponent!=null) {
                    this.textBox1.Text = existingComponent.iri;
                }
            }
        }

        private string generateId()
        {
            int i = 0;
            OpenDoPE_Wed.componentsComponent fetchedComponent;
            do
            {
                i++;
                fetchedComponent = getComponentById("comp" + i);

            } while (fetchedComponent != null);

            return "comp" + i;

        }

        private OpenDoPE_Wed.componentsComponent getComponentById(string id)
        {
            foreach( OpenDoPE_Wed.componentsComponent comp in odComponents.component) {
                if (comp.id.Equals(id)) {
                    return comp;
                }
            }
            return null;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            Word.ContentControl currentCC = taskPane.currentCC;

            if (existingComponent == null)
            {
                existingComponent = new componentsComponent();
                existingComponent.id = generateId();
                odComponents.component.Add(existingComponent);
            }
            existingComponent.iri = this.textBox1.Text;

            // Write tag
            TagData td = new TagData(currentCC.Tag);
            td.remove("od:repeat");
            td.remove("od:condition");
            td.remove("od:component");
            td.set("od:component", existingComponent.id);
            currentCC.Tag = td.asQueryString();

            // Save it in docx
            string result = odComponents.Serialize();
            log.Info(result);
            CustomXmlUtilities.replaceXmlDoc(taskPane.componentsPart, result);

            log.Debug("Component written!");
            Mbox.ShowSimpleMsgBoxInfo("Component written!");

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {

            Word.ContentControl currentCC = taskPane.currentCC;

            if (currentCC == null)
            {
                log.Error("currentCC not set?!");
                return;
            }

            // Write tag
            TagData td = new TagData(currentCC.Tag);

            try
            {
                if (td.get("od:component") == null)
                {
                    Mbox.ShowSimpleMsgBoxInfo("This content control isn't a component.");
                    return;
                }
            }
            catch (Exception ex)
            {
            }

            td.remove("od:component");
            currentCC.Tag = td.asQueryString();

            Mbox.ShowSimpleMsgBoxInfo("component removed from this content control");



        }
    }
}
